package org.lcc.mapper;

import org.apache.ibatis.annotations.*;
import org.lcc.model.VO.NotificationVO;
import org.lcc.model.entity.Notification;
import org.lcc.model.entity.NotificationReceiver;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface NotificationMapper {
    
    /**
     * 插入通知
     */
    @Insert("INSERT INTO notification(title, content, sender_id, notification_type, send_time, status, priority) " +
            "VALUES(#{title}, #{content}, #{senderId}, #{notificationType}, #{sendTime}, #{status}, #{priority})")
    @Options(useGeneratedKeys = true, keyProperty = "notificationId")
    int insertNotification(Notification notification);
    
    /**
     * 插入通知接收者
     */
    @Insert("INSERT INTO notification_receiver(notification_id, target_type, target_id, read_status) " +
            "VALUES(#{notificationId}, #{targetType}, #{targetId}, #{readStatus})")
    int insertNotificationReceiver(NotificationReceiver receiver);
    
    /**
     * 批量插入通知接收者
     */
    int batchInsertReceivers(@Param("receivers") List<NotificationReceiver> receivers);
    
    /**
     * 获取所有通知（管理员使用）
     */
    @Select("SELECT n.notification_id, n.title, n.content, n.sender_id, n.notification_type, n.send_time, n.status, n.priority, " +
            "u.real_name as sender_name " +
            "FROM notification n " +
            "LEFT JOIN user u ON n.sender_id = u.school_number " +
            "WHERE n.status = 'active' " +
            "ORDER BY n.send_time DESC " +
            "LIMIT #{pageSize} OFFSET #{offset}")
    List<NotificationVO> getAllNotifications(@Param("offset") int offset, @Param("pageSize") int pageSize);
    
    /**
     * 获取所有通知总数
     */
    @Select("SELECT COUNT(*) FROM notification WHERE status = 'active'")
    int countAllNotifications();
    
    /**
     * 获取学生的通知列表
     */
    List<NotificationVO> getStudentNotifications(@Param("studentId") String studentId, 
                                               @Param("offset") int offset, 
                                               @Param("pageSize") int pageSize);
    
    /**
     * 获取学生的通知总数
     */
    int countStudentNotifications(@Param("studentId") String studentId);
    
    /**
     * 获取教师发送的通知列表
     */
    @Select("SELECT n.notification_id, n.title, n.content, n.sender_id, n.notification_type, n.send_time, n.status, n.priority, " +
            "u.real_name as sender_name " +
            "FROM notification n " +
            "LEFT JOIN user u ON n.sender_id = u.school_number " +
            "WHERE n.sender_id = #{teacherId} AND n.status = 'active' " +
            "ORDER BY n.send_time DESC " +
            "LIMIT #{pageSize} OFFSET #{offset}")
    List<NotificationVO> getTeacherNotifications(@Param("teacherId") String teacherId, 
                                               @Param("offset") int offset, 
                                               @Param("pageSize") int pageSize);
    
    /**
     * 获取教师发送的通知总数
     */
    @Select("SELECT COUNT(*) FROM notification WHERE sender_id = #{teacherId} AND status = 'active'")
    int countTeacherNotifications(@Param("teacherId") String teacherId);
    
    /**
     * 获取通知详情
     */
    NotificationVO getNotificationDetail(@Param("notificationId") Integer notificationId, 
                                       @Param("studentId") String studentId);
    
    /**
     * 标记通知为已读
     */
    @Update("UPDATE notification_receiver SET read_status = 1, read_time = #{readTime} " +
            "WHERE notification_id = #{notificationId} AND target_type = 'student' AND target_id = #{studentId}")
    int markAsRead(@Param("notificationId") Integer notificationId, 
                  @Param("studentId") String studentId, 
                  @Param("readTime") LocalDateTime readTime);
    
    /**
     * 获取学生未读通知数量
     */
    @Select("SELECT COUNT(*) FROM notification n " +
            "JOIN notification_receiver nr ON n.notification_id = nr.notification_id " +
            "WHERE nr.target_type = 'student' AND nr.target_id = #{studentId} " +
            "AND nr.read_status = 0 AND n.status = 'active'")
    int countUnreadNotifications(@Param("studentId") String studentId);
    
    /**
     * 删除通知
     */
    @Update("UPDATE notification SET status = 'inactive' WHERE notification_id = #{notificationId}")
    int deleteNotification(@Param("notificationId") Integer notificationId);
    
    /**
     * 获取班级学生列表
     */
    @Select("SELECT school_number FROM student_class_link WHERE class_id = #{classId}")
    List<String> getClassStudents(@Param("classId") String classId);
} 